package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.Common.R;
import com.wyx.Pojo.Category;
import com.wyx.Pojo.Dish;
import com.wyx.Pojo.Setmeal;
import com.wyx.Service.CategoryService;
import com.wyx.Service.DishService;
import com.wyx.Service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 目录、品种信息模块
 */
@RestController
@RequestMapping("/category")
public class CategoryCotroller {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功~");
    }
    @GetMapping("/page")
    public R<Page>getpage(int page,int pageSize){
        //记得加泛型
        Page <Category>page1=new Page<>(page,pageSize);
        //按照Sort升序排序
        LambdaQueryWrapper<Category>wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(page1,wrapper);
        return R.success(page1);
    }

    /**
     * 根据id删除某一种分类
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> deletebyids(Long ids){

        int deletebyids = categoryService.deletebyids(ids);
        if (deletebyids>0){
            return R.success("删除成功~");
        }else {
            return R.error("删除失败~");
        }
    }

    /**
     * 根据传进来的category修改品种
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 获取全部种类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> getlist(Category category){
        if (category.getType()!=null){
            LambdaQueryWrapper <Category> wrapper=new LambdaQueryWrapper<>();
            //类型条件筛选套餐跟菜品
            wrapper.eq(category.getType()!=null,Category::getType,category.getType());
            //排序条件
            wrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);
            List<Category> list = categoryService.list(wrapper);
            return R.success(list);
        }else {
            //说明是前端发来的
            LambdaQueryWrapper <Category> wrapper=new LambdaQueryWrapper<>();
            wrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);
            List<Category> list = categoryService.list(wrapper);
            List<Category>res=new ArrayList<>();
            for (Category category1 : list) {
                //说明是菜品
                if (category1.getType()==1){
                    LambdaQueryWrapper<Dish>wrapper1=new LambdaQueryWrapper<>();
                    wrapper1.eq(Dish::getCategoryId,category1.getId());
                    if (dishService.count(wrapper1)!=0){
                        res.add(category1);
                    }
                }
                //说明是套餐
                if (category1.getType()==2){
                    LambdaQueryWrapper<Setmeal>wrapper1=new LambdaQueryWrapper<>();
                    wrapper1.eq(Setmeal::getCategoryId,category1.getId());
                    if (setmealService.count(wrapper1)!=0){
                        res.add(category1);
                    }
                }

            }
            return R.success(res);
        }
    }
}
